package algorithm;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//给定一个整数数组nums，从数组中找出两个数满足相加之和等于目标target
//假设每个输入只对应唯一的答案，而且不可以重复使用相同的元素
//返回两数的下标值，以数组的形式返回
public class Test {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(solution1(new int[]{1,2,3,4,5,6}, 10)));
//        System.out.println( Arrays.toString(solution2(new int[]{5,6,7,8,1,3,4,2},10)) );
    }

    public static int[] solution2(int[] nums,int target){
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i <nums.length; i++) {
            if(map.containsKey(target - nums[i])){
                return new int[]{map.get(target - nums[i]),i};
            }
            map.put(nums[i],i);
        }
        return new int[0];
    }

    public static int[] solution(int[] nums, int target){
        for(int i=0;i<nums.length;i++){
            for (int j=i+1;j<nums.length;j++){
                if(nums[i]+nums[j] == target){
                    return new int[]{i,j};
                }
            }
        }
        return new int[0];
    }

    public static int[] solution1(int[] nums, int target){
        Map<Integer, Integer> map = new HashMap<>();
        for (int i=1;i<nums.length;i++){
            if(map.containsKey(target - nums[i])){
                return new int[]{map.get(target - nums[i]),i};
            }
            map.put(nums[i],i);
        }
        return new int[0];
    }
}
